Ext.define("Pearlsen.Page",{
	basePath:"/",
    title:"",
    viewUrl:"/view.jsp",
    addUrl:"/add.jsp",
    editUrl:"/edit.jsp",
    gridId:'pageGrid',
    formId:'pageFrom',
    delHandler:"",
    dataSet:"",
    delMethod:"delete",
	constructor : function(config){
		Ext.apply(this,config,true);
	 },
	view:function(obj,e){
		var me = this;
    	var record =  me.getCurrentRecord();
    	if(!record||record.length!=1){
    		Pearlsen.Utils.showWarnMsg("请选择一条记录。");
    		return;
    	}
    	Pearlsen.Utils.addTab(me.title+"_view","查看_"+me.title,me.basePath+me.viewUrl);
    },
    add:function(obj,e){
		var me = this;
    	Pearlsen.Utils.addTab(me.title+"_add","新增_"+me.title,me.basePath+me.addUrl);
    },
    edit:function(obj,e){
    	var me = this;
    	var record =  me.getCurrentRecord();
    	if(!record||record.length!=1){
    		Pearlsen.Utils.showWarnMsg("请选择一条记录。");
    		return;
    	}
    	window.parent.Pearlsen.Page = Pearlsen.Page;
    	Pearlsen.Utils.addTab(me.title+"_edit","编辑_"+me.title,me.basePath+me.editUrl);
    },
    del:function(obj,e){
    	var me = this;
    	var record =  me.getCurrentRecord();
    	if(!record||record<1){
    		Pearlsen.Utils.showWarnMsg("请选择记录。");
    		return;
    	}
    	Ext.MessageBox.confirm('Confirm', '确定要删除所选数据吗?',function(btn){
     	 	if(btn=='yes'){
     	 	var ds=new DataSet(me.delHandler,me.dataSet);
  			ds.setHandlerMethod(me.delMethod);
  			for(var i=0;i<record.length;i++){
  				ds.newRecord();
  				ds.setValue(record[i].idProperty,record[i].getId());
  			}
 			ds.execute();
 			me.reloadGrid();
     	 }
		});
    	
    },
    /**
     * 获取表格选中的行
     * @memberOf {TypeName} 
     * @return {TypeName} 
     */
    getCurrentRecord:function(){
    	var me = this;
		var record = me.getGrid().getSelectionModel().getSelection();
		if(record){
			Pearlsen.Page.currentRecord = record;
			return record;
		}
		return false;
	},
	getGrid:function(){
		if(!this.grid){
			this.grid = Ext.getCmp(this.gridId);
		}
		return this.grid;
	},
	/**
	 * 刷新表格
	 */
	reloadGrid:function(){
		var me = this;
		me.getGrid().getStore().load();
	}
});

Pearlsen.Page2 = {
	basePath:"/",
    title:"",
    viewUrl:"/view.jsp",
    addUrl:"/add.jsp",
    editUrl:"/edit.jsp",
    gridId:'pageGrid',
    formId:'pageFrom',
    delHandler:"",
    dataSet:"",
    delMethod:"delete",
    view:function(obj,e){
		var me = Pearlsen.Page;
    	var record =  me.getCurrentRecord();
    	if(!record||record.length!=1){
    		Pearlsen.Utils.showWarnMsg("请选择一条记录。");
    		return;
    	}
    	window.parent.Pearlsen.Page = Pearlsen.Page;
    	Pearlsen.Utils.addTab(me.title+"_view","查看_"+me.title,me.basePath+me.viewUrl);
    },
    add:function(obj,e){
		var me = Pearlsen.Page;
    	Pearlsen.Utils.addTab(me.title+"_add","新增_"+me.title,me.basePath+me.addUrl);
    },
    edit:function(obj,e){
    	var me = Pearlsen.Page;
    	var record =  me.getCurrentRecord();
    	if(!record||record.length!=1){
    		Pearlsen.Utils.showWarnMsg("请选择一条记录。");
    		return;
    	}
    	window.parent.Pearlsen.Page = Pearlsen.Page;
    	Pearlsen.Utils.addTab(me.title+"_edit","编辑_"+me.title,me.basePath+me.editUrl);
    },
    del:function(obj,e){
    	var me = Pearlsen.Page;
    	var record =  me.getCurrentRecord();
    	if(!record||record<1){
    		Pearlsen.Utils.showWarnMsg("请选择记录。");
    		return;
    	}
    	Ext.MessageBox.confirm('Confirm', '确定要删除所选数据吗?',function(btn){
     	 	if(btn=='yes'){
     	 	var ds=new DataSet(me.delHandler,me.dataSet);
  			ds.setHandlerMethod(me.delMethod);
  			for(var i=0;i<record.length;i++){
  				ds.newRecord();
  				ds.setValue(record[i].idProperty,record[i].getId());
  			}
 			ds.execute();
 			me.reloadGrid();
     	 }
		});
    	
    },
    /**
     * 获取表格选中的行
     * @memberOf {TypeName} 
     * @return {TypeName} 
     */
    getCurrentRecord:function(){
    	var me = Pearlsen.Page;
		var record = me.getGrid().getSelectionModel().getSelection();
		if(record){
			Pearlsen.Page.currentRecord = record;
			return record;
		}
		return false;
	},
	getGrid:function(){
		if(!this.grid){
			this.grid = Ext.getCmp(this.gridId);
		}
		return this.grid;
	},
	/**
	 * 刷新表格
	 */
	reloadGrid:function(){
		var me = Pearlsen.Page;
		me.getGrid().getStore().load();
	}

};